﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComboGrid
{
    public partial class ComboGrid : UserControl
    {
        private Label[] titleElements;
        private ComboBox[,] elements;
        private Point selectedCoor;

        private const int space = 2;

        public ComboGrid()
        {
            InitializeComponent();
            Prepare(3, 3);
        }

        public void Clear()
        {
            int i, j;

            for (i = 0; i < titleElements.Length; i++)
            {
                titleElements[i].Dispose();
            }

            for (i = 0; i < elements.GetLength(0); i++)
            {
                for (j = 0; j < elements.GetLength(1); j++)
                {
                    elements[i, j].Dispose();
                }
            }

            titleElements = null;
            elements = null;
            selectedCoor = new Point(-1, -1);
        }

        public void Prepare(int rows, int columns)
        {
            Prepare(rows, columns, 22, 85);
        }

        public void Prepare(int rows, int columns, int rowSize, int colSize)
        {
            int row, column;

            if (titleElements != null || elements != null)
            {
                Clear();
            }

            titleElements = new Label[columns];
            for (column = 0; column < columns; column++)
            {
                Label t;
                t = new Label();
                t.Parent = this;
                t.Left = (column * (colSize + ComboGrid.space)) + ComboGrid.space;
                t.Top = 0;

                //t.AutoSize = true;
                t.AutoSize = false;
                t.TextAlign = ContentAlignment.BottomCenter;
                t.Width = colSize;
                t.Height = rowSize * 2;
                //t.BorderStyle = BorderStyle.FixedSingle;

                titleElements[column] = t;
            }

            elements = new ComboBox[rows, columns];
            for (row = 0; row < rows; row++)
            {
                for (column = 0; column < columns; column++)
                {
                    ComboBox c;
                    c = new ComboBox();
                    c.Parent = this;
                    c.Left = (column * (colSize + ComboGrid.space)) + ComboGrid.space;
                    c.Top = (row + 2) * (rowSize + ComboGrid.space);
                    c.DropDownStyle = ComboBoxStyle.DropDown;
                    c.Tag = row.ToString() + "," + column.ToString();
                    c.GotFocus += new EventHandler(element_GotFocus);
                    c.Width = colSize;
                    c.Height = rowSize;
                    elements[row, column] = c;
                }
            }
        }

        private void element_GotFocus(object sender, EventArgs e)
        {
            ComboBox c = (ComboBox)sender;
            string[] parts;

            parts = c.Tag.ToString().Split(',');

            selectedCoor.X = Convert.ToInt32(parts[1]);
            selectedCoor.Y = Convert.ToInt32(parts[0]);

            //MessageBox.Show(c.Tag.ToString());
        }

        public ComboBox[,] Cells
        {
            get
            {
                return elements;
            }
        }

        public Label[] TitleCells
        {
            get
            {
                return titleElements;
            }
        }

        public Point SelectedElement
        {
            get
            {

                return selectedCoor;
            }
        }

        public string[] Titles
        {
            get
            {
                string[] arr;
                arr = new string[titleElements.Length];

                for (int i = 0; i < titleElements.Length; i++)
                {
                    arr[i] = titleElements[i].Text;
                }
                                
                return arr;
            }

            set
            {
                if (value != null)
                {
                    for (int i = 0; (i < value.Length) && (i < titleElements.Length); i++)
                    {
                        titleElements[i].Text = value[i];
                    }
                }
            }
        }

        public ComboBox CellAt(int row, string colTitle)
        {
            int i, col;
            col = -1;
            for (i = 0; i < titleElements.Length; i++)
            {
                if (titleElements[i].Text == colTitle)
                {
                    col = i;
                    break;
                }
            }

            if (col == -1)
            {
                throw new Exception("Could not find the column.");
            }


            return elements[row, col];
        }

        public int RowsCount
        {
            get
            {
                if (elements == null) return 0;
                return elements.GetLength(0);
            }

            set
            {
                if (elements == null)
                {
                    Prepare(value, 3);
                }
                else
                {
                    Prepare(value, elements.GetLength(1));
                }
            }
        }

        public int ColumnsCount
        {
            get
            {
                if (elements == null) return 0;
                return elements.GetLength(1);
            }

            set
            {
                if (elements == null)
                {
                    Prepare(3, value);
                }
                else
                {
                    Prepare(elements.GetLength(0), value);
                }
            }
        }

        public ComboBox[] CellsOfColumn(string colTitle)
        {
            int nrows;
            nrows = RowsCount;
            ComboBox[] result = new ComboBox[nrows];

            for (int i = 0; i < nrows; i++)
            {
                result[i] = CellAt(i, colTitle);
            }

            return result;
        }
    }
}